package 集合.queueExamples;

import java.awt.print.PrinterGraphics;
import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;

public class QuqueUse {
	public static void main(String[] args) {
		offerTest(); // 有限队列、增加元素
		removeTest(); // 移除元素
		pollTest(); //队列结构，删除头部
		elementTest();//检索头部，但是不删除头部。若为空，则异常
		peekTest();//检索头部，但是不删除头部。若为空，则返回空
	}

	private static void peekTest() {
		LinkedList<Integer> subList = generatorList(3);
		subList.clear();
		Integer element = subList.peek();
		System.out.println(element);
		System.out.println(subList);
	}

	private static void elementTest() {
		LinkedList<Integer> subList = generatorList(3);
		Integer element = subList.element();
		System.out.println(element);
		System.out.println(subList);
	}

	private static void pollTest() {
		LinkedList<Integer> subList = generatorList(3);
		subList.poll();
		System.out.println(subList);
	}

	private static void removeTest() {
		LinkedList<Integer> subList = generatorList(3);
		subList.remove(1); //根据 index
		LinkedList<String> strings = new LinkedList<>();
		strings.add("test");
		strings.remove("test"); //根据类型
		System.out.println(subList);
		System.out.println(strings);
	}

	private static void offerTest() {
		LinkedBlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>(3);
		blockingQueue.offer(1);
		blockingQueue.offer(2);
		blockingQueue.offer(3);
		// 4/5都无法插入了，因为容量满了
		blockingQueue.offer(4);
		blockingQueue.offer(5);
		//Queue full 队列已经满了
		//blockingQueue.add(4);
		System.out.println(blockingQueue);
	}

	// 保留交集数据
	private static void retainAllTest(LinkedList<Integer> integerLinkedList, LinkedList<Integer> subList) {
		boolean b = integerLinkedList.retainAll(subList);
		System.out.println(b);
		System.out.println(subList); //[0,1,2]
		System.out.println(integerLinkedList); //[0,1,2]
	}

	private static LinkedList<Integer> generatorList(int length) {
		LinkedList<Integer> linkedList = new LinkedList<>();
		for (int i = 0; i < length; i++) {
			linkedList.add(i);
		}
		return linkedList;
	}
}
